﻿using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using ERP.Team.RBAC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-质量管理-出货检验-xzc
    /// </summary>
    public class OutgoingInspectionServices : ApplicationService, IOutgoingInspectionServices
    {
        private readonly IRepository<OutgoingInspection> _outgoinginspectionRepository;
        private readonly IRepository<UserModel> _usermodelRepository;

        public OutgoingInspectionServices(IRepository<OutgoingInspection> outgoinginspectionRepository, IRepository<UserModel> usermodelRepository)
        {
            _outgoinginspectionRepository = outgoinginspectionRepository;
            _usermodelRepository = usermodelRepository;
        }
        /// <summary>
        /// 新增出货检验
        /// </summary>
        /// <param name="outgoinginspectiondto"></param>
        /// <returns></returns>
        public async Task<int> AddOutgoingInspection(OutgoingInspectionDto outgoinginspectiondto)
        {
            var i = ObjectMapper.Map<OutgoingInspectionDto, OutgoingInspection>(outgoinginspectiondto);
            var outgoinginspectionList = await _outgoinginspectionRepository.InsertAsync(i);
            return outgoinginspectionList == null ? 0 : 1;
        }
        /// <summary>
        /// 通过id查询刀出货检验数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<OutgoingInspection>> FindOutgoingInspection(int id)
        {
            var outgoinginspectionList = await _outgoinginspectionRepository.GetListAsync(m => m.Id == id);
            return outgoinginspectionList;
        }
        /// <summary>
        /// 更新出货检验
        /// </summary>
        /// <param name="outgoinginspectiondto"></param>
        /// <returns></returns>
        public async Task<int> UpdateOutgoingInspection(OutgoingInspectionDto outgoinginspectiondto)
        {
            var i = ObjectMapper.Map<OutgoingInspectionDto, OutgoingInspection>(outgoinginspectiondto);
            var outgoinginspectionList = await _outgoinginspectionRepository.UpdateAsync(i);
            return outgoinginspectionList == null ? 0 : 1;
        }
        /// <summary>
        /// 删除出货检验
        /// </summary>
        /// <param name="outgoinginspectiondto"></param>
        /// <returns></returns>
        public async Task DeleteOutgoingInspection(int id)
        {
            await _outgoinginspectionRepository.DeleteAsync(m => m.Id == id);
        }
        /// <summary>
        /// 批量删除出货检验
        /// </summary>
        /// <param name="ids">要删除的ID列表</param>
        /// <returns></returns>
        public async Task<int> BatchDeleteOutgoingInspection(List<int> ids)
        {
            if (ids == null || ids.Count == 0)
            {
                return 0;
            }

            try
            {
                // 批量删除
                await _outgoinginspectionRepository.DeleteAsync(m => ids.Contains(m.Id));
                return ids.Count;
            }
            catch (Exception)
            {
                // 发生错误时返回0
                return 0;
            }
        }
        /// <summary>
        /// 查询出货检验
        /// </summary>
        /// <param name="getoutgoinginspectiondto"></param>
        /// <returns></returns>
        public async Task<List<OutgoingInspection>> GetOutgoingInspection(GetOutgoingInspectionDto getoutgoinginspectiondto)
        {
            var outgoinginspectionList = await _outgoinginspectionRepository.GetListAsync();
            outgoinginspectionList = outgoinginspectionList.WhereIf(!string.IsNullOrEmpty(getoutgoinginspectiondto.OutgoingCode), m => m.OutgoingCode == getoutgoinginspectiondto.OutgoingCode).ToList();
            outgoinginspectionList = outgoinginspectionList.WhereIf(!string.IsNullOrEmpty(getoutgoinginspectiondto.CustomerName), m => m.CustomerName.Contains(getoutgoinginspectiondto.CustomerName)).ToList();
            outgoinginspectionList = outgoinginspectionList.WhereIf(!string.IsNullOrEmpty(getoutgoinginspectiondto.Batch), m => m.Batch == getoutgoinginspectiondto.Batch).ToList();
            outgoinginspectionList = outgoinginspectionList.WhereIf(!string.IsNullOrEmpty(getoutgoinginspectiondto.ProductName), m => m.ProductName.Contains(getoutgoinginspectiondto.ProductName)).ToList();
            outgoinginspectionList = outgoinginspectionList.WhereIf(!string.IsNullOrEmpty(getoutgoinginspectiondto.ProductCode), m => m.ProductCode == getoutgoinginspectiondto.ProductCode).ToList();
            outgoinginspectionList = outgoinginspectionList.WhereIf(!string.IsNullOrEmpty(getoutgoinginspectiondto.TestingResult), m => m.TestingResult == getoutgoinginspectiondto.TestingResult).ToList();
            return outgoinginspectionList;
        }
        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserModel>> GetUserList(GetUserListDto getuserlistdto)
        {
            var userList = await _usermodelRepository.GetListAsync();
            userList = userList.WhereIf(!string.IsNullOrEmpty(getuserlistdto.UserName), m => m.UserName.Contains(getuserlistdto.UserName)).ToList();
            userList = userList.WhereIf(!string.IsNullOrEmpty(getuserlistdto.UserNickName), m => m.UserNickName.Contains(getuserlistdto.UserNickName)).ToList();
            return userList;
        }
    }
}
